<?php
/**
 * User: xzq
 * Date: 2018/8/14
 * Time: 16:26
 */

namespace app\library\home;
use app\common\controller\Common;
use app\library\model\User as UserModel;
use think\captcha\Captcha;
class Login extends Common
{
    /**
     * 登入页面
     * @return string
     */
    public function index(){
        $user = new UserModel();
        if ($this->request->isPost()) {
            $username = input('post.card_id/s');
            $password = input('post.password/s');
            if (!$user->login($username, $password)) {
                return $this->error($user->getError(), url('index'));
            }
            return $this->success('登陆成功，页面跳转中...',ROOT_DIR);
        }
        //判断是否登入
        if ($user->isLogin()) {
            $this->redirect(ROOT_DIR);
        }
        return $this->fetch();
    }
    /**
     * 生成验证码
     */
    public function code(){
        $config =    [
            'fontSize'    =>    36,
            'useCurve'    =>    true,
            'length'      =>    4,
            'useNoise'    =>    false,
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }
    /**
     * 退出登陆
     * @return mixed
     */
    public function logout(){
        $user = new UserModel();
        $user->logout();
        $this->redirect(url('login/index', '', true, true));
    }
}